﻿using System;
using Kiva7.Translations;
using Kiva7.Utilities;
using Kiva7.Constants;
using System.ComponentModel;
using Kiva7.ViewModels;

namespace Kiva7.Baskets
{
    public class Basket : BaseViewModel
    {

        private void NotifyProperties()
        {
            NotifyPropertyChanged(_isEmptyConst);
            NotifyPropertyChanged(_buttonTextConst);
        }

        private readonly string _isEmptyConst = Name.Of<Basket>(x => x.IsEmpty);
        public bool IsEmpty
        {
            get
            {
                return _items.Count == 0;
            }
        }

        public bool Filled
        {
            get
            {
                return _items.Count != 0;
            }
        }

        private readonly string _buttonTextConst = Name.Of<Basket>(x => x.ButtonText);
        public string ButtonText
        {
            get
            {
                if (_items.Count == 0)
                {
                    return "Basket is empty.";
                }
                else if (_items.Count == 0)
                {
                    return "Send basket with 1 item";
                }
                else
                {
                    return string.Format("Send basket with {0} items", _items.Count);
                }
            }
        }

        private static Basket _current;
        public static Basket Current
        {
            get
            {
                if (_current == null)
                {
                    _current = new Basket();
                }
                return _current;
            }
        }

        private readonly BasketItemList _items = new BasketItemList();
        
        public void AddItem(int loanId)
        {
            if (_items.ContainsKey(loanId))
                {
                    return;
                }


            _items.Add(loanId, new BasketItem { LoanId = loanId });
            NotifyProperties();
        }

        public void Clear()
        {
            _items.Clear();
            NotifyProperties();
        }

        public string GetHtml()
        {
            return string.Format(Html, _items, 0, Constant.KivaAppId, AppResources.ConnectingToKiva);
        }
            
            private const string Html = @"
<html>
<head>
<title>KIVA Basket</title>
</head>
<body>
<form id=""a"" method=""post"" action=""http://www.kiva.org/basket/set"">
  <input name=""loans"" value='{0}' type=""hidden"" />
  <input name=""donation"" value=""{1}"" type=""hidden"" />
  <input name=""app_id"" value=""{2}"" type=""hidden"" />

<table style=""height:100%;width:100%"">

<tr style=""height:50%;"">
<td style=""height:50%;"">
&nbsp;
</td>
</tr>

<tr style=""height:1"">
<td style=""height:1;font-size:100;color:Green"" align=""center"">
{3}
</td>
</tr>

<tr style=""height:50%;"">
<td style=""height:50%;"">
&nbsp;
</td>
</tr>

</table>
</form>
<script type=""text/javascript"">
    document.forms[""a""].submit();
</script>
</body>
</html>
";



            public bool Contains(int loadId)
            {
                return _items.ContainsKey(loadId);
            }
    }
}
